package com.hspedu.newData.dmsxl.回溯.组合;

import org.junit.Test;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * @Author: yh
 * @description: 
 * @CreateTime: 2025-04-26 
 * @Version: 1.0
 */

public class LC216组合总和Ⅲ {

    LinkedList<Integer> path = new LinkedList<>();
    List<List<Integer>> res = new ArrayList<>();

    public List<List<Integer>> combinationSum3(int k, int n) {
        backtracking(n,k,1);
        return res;
    }

    public void backtracking(int n,int k,int startIndex){
        if (n < 0){
            return;
        }
        if (path.size() == k){
            if (n == 0){
                res.add(new ArrayList<>(path));
            }
            return;
        }

        for (int i = startIndex; i <= 9 - (k - path.size() + 1); i++) {
            path.add(i);
            backtracking(n-i,k,i+1);
            path.removeLast();
        }
    }


    @Test
    public void test() {
        // 测试代码
        List<List<Integer>> lists = combinationSum3(3, 9);
        System.out.println("===========");
    }
}
